<?php
declare (strict_types = 1);

namespace app\admin\controller;
use app\admin\middleware\Auth;
use app\model\CategoryModel;
use app\model\CourseMenusModel;
use app\model\CourseModel;
use app\model\MenusModel;
use app\model\RedisModel;
use think\App;
use think\facade\Db;

class CourseMenus extends Base {

    protected $middleware = [Auth::class];
    private $courseMenusModel;

    private $req;

    private $page;

    private $limit;


    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->req = request()->param();

        $this->page = isset($this->req['page']) ? $this->req['page'] : 1;

        $this->limit = isset($this->req['limit']) ? $this->req['limit'] : 999999;

        $this->courseMenusModel = new CourseMenusModel();

        $this->isLogin();
    }

    /*
     * 获取店铺列表
     * */
    public function getList(){

        $total = $this->courseMenusModel->getTotal();
        $list = $this->courseMenusModel->getList([],$this->page,$this->limit);

        $courseModel = new CourseModel();
        foreach ($list as &$item){
            $item["course_num"] = $courseModel->getTotal([
                "c.course_menus_id"=>$item["course_menus_id"],
                "c.is_delete"=>0
            ]);
        }
        return $this->succData($total,$list);

    }


    public function add()
    {

        if(!isset($this->req['course_menus_name']) || empty($this->req['course_menus_name'])){
            return $this->fail("请输入课程类目名称");
        }

//        if(!isset($this->req['course_menus_icon']) || empty($this->req['course_menus_icon'])){
//            return $this->fail("请上传icon");
//        }
//
//        if(!isset($this->req['course_menus_url']) || empty($this->req['course_menus_url'])){
//            return $this->fail("请输入跳转链接");
//        }


        $addres = $this->courseMenusModel->insertGetId([
            "course_menus_name" => $this->req['course_menus_name'],
            "course_menus_icon" => isset($this->req['course_menus_icon']) ? $this->req['course_menus_icon'] : "",
            "course_menus_url" =>  isset($this->req['course_menus_url']) ? $this->req['course_menus_url'] : "",
            "course_menus_sort" =>isset($this->req['course_menus_sort']) ? $this->req['course_menus_sort'] : 0,
        ]);

        if($addres){
            $this->delAllCache();
            return $this->succ();
        }else{
            return $this->fail();
        }
    }



    public function edit()
    {
        if(!isset($this->req['course_menus_id']) || empty($this->req['course_menus_id'])){
            return $this->fail("请选择类目");
        }

        if(!isset($this->req['course_menus_name']) || empty($this->req['course_menus_name'])){
            return $this->fail("请输入课程类目名称");
        }

//        if(!isset($this->req['course_menus_icon']) || empty($this->req['course_menus_icon'])){
//            return $this->fail("请上传icon");
//        }
//
//        if(!isset($this->req['course_menus_url']) || empty($this->req['course_menus_url'])){
//            return $this->fail("请输入跳转链接");
//        }


        $addres = $this->courseMenusModel->where("course_menus_id",$this->req['course_menus_id'])->save([
            "course_menus_name" => $this->req['course_menus_name'],
            "course_menus_icon" => isset($this->req['course_menus_icon']) ? $this->req['course_menus_icon'] : "",
            "course_menus_url" =>  isset($this->req['course_menus_url']) ? $this->req['course_menus_url'] : "",
            "course_menus_sort" => isset($this->req['course_menus_sort']) ? $this->req['course_menus_sort'] : 0,
            "course_menus_status" => isset($this->req['course_menus_status']) ? $this->req['course_menus_status'] : 1,
        ]);

        if($addres){
            $this->delAllCache();
            return $this->succ();
        }else{
            return $this->fail();
        }
    }



    public function del()
    {
        if(!isset($this->req['course_menus_id']) || empty($this->req['course_menus_id'])){
            return $this->fail("请选择类目");
        }

        $res = $this->courseMenusModel->where("course_menus_id",$this->req['course_menus_id'])->save([
            "is_delete"=>1
        ]);
        if($res){
            $this->delCache($this->req['course_menus_id']);
            return $this->succ();
        }else{
            return $this->fail();
        }
    }


    public function delCache($id)
    {
        $redisModel = new RedisModel();

        return $redisModel->delete( $this->courseMenusModel->cacheKey.$id);
    }


    public function delAllCache()
    {
        $redisModel = new RedisModel();

        return $redisModel->deleteAll( $this->courseMenusModel->cacheKey);
    }

}
